<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\user;

use app\api\model\dao\Circle;
use app\api\model\dao\UpRank;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\library\Common;
use app\library\CommonConfig;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetUpRank extends PageBase
{

    private $objCircle;
    private $objUpRank;
    private $objUserAccount;
    private $objUserCurrency;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objUpRank = new UpRank();
        $this->objUserAccount = new UserAccount();
        $this->objUserCurrency = new UserCurrency();

    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'tab' => 'require',
        ];
        $msg = [
            'tab.require' => '参数缺少tab',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function myExcute($param)
    {
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            throw new Exception($ret, Error::ERR_PARAM);
        }

        $this->data = $this->getUpRank($param['tab'], date('Ymd'));

    }

    /**
     * 帖子消费榜
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getUpRank($tab, $date = 0)
    {
        if (empty($date)) {
            return [];
        }

        if (Flag::CONST_RANK_TAB_TODAY == $tab) {
            $ret = $this->objUpRank->getUpRankByDay($date);
        } else if (Flag::CONST_RANK_TAB_WEEK == $tab) {
            $ret = $this->objUpRank->getUpRankByWeek();
        } else {
            $ret = $this->objUpRank->getUpRankTotal();
        }

        if (empty($ret)) {
            return [];
        }

        $userid = [];
        $userid2Rank = [];

        foreach ($ret as $val) {
            $userid[] = $val['userid'];
            $userid2Rank[$val['userid']] = $val;
        }

        $user_account = [];
        $user_integral = [];

        if (!empty($userid)) {
            $user_account_ret = $this->objUserAccount->getUserAccountByUserids($userid, ['id', 'nick_name', 'sex', 'level', 'avatar']);
            if (empty($user_account_ret)) {
                return [];
            }
            $user_account_ret = Common::modelToArray($user_account_ret);
            foreach ($user_account_ret as $user) {
                $user_account[$user['id']] = $user;
            }
            $user_integral_ret = $this->objUserCurrency->getCurrencyByUserids($userid, Flag::CURENCY_TYPE_INTEGRAL);
            if (!empty($user_integral_ret)) {
                $user_integral_ret = Common::modelToArray($user_integral_ret);
            } else {
                $user_integral_ret = [];
            }
            foreach ($user_integral_ret as $integral) {
                $user_integral[$integral['user_id']] = $integral['amount'];
            }
        }


        $data = [];

        $rank = 1;

        foreach ($userid2Rank as $key => $val) {

            if (empty($user_account[$key])) {
                continue;
            }

            $integral = !empty($user_integral[$key]) ? $user_integral[$key] : 0;
            $level = Common::conversionLevel($integral);

            $data[] = [
                'rank' => $rank++,
                'userid' => $user_account[$key]['id'],
                'name' => $user_account[$key]['nick_name'],
                'photo' => $user_account[$key]['avatar'] ? $user_account[$key]['avatar'] : CommonConfig::DEFAULT_AVATAR,
                'sex' => 1 == $user_account[$key]['sex'] ? 'F' : 'M',
                'level' => $level,
                'value' => $val['up_value']
            ];
        }

        return $data;

    }

}
